import 'package:color_dart/RgbaColor.dart';

/// home.dart
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/pages/tab/home/page.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/alliance/home.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/message/index.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/mine/index.dart';
import 'package:im_flutter_sdk_example/provider/app_model.dart';
import 'package:im_flutter_sdk_example/provider/group_formdate_model.dart';
import 'package:im_flutter_sdk_example/provider/home_model.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:provider/provider.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var tabImages = [];
  final bodyList = [FirstPage(), ActPage(), Message(), Mine()];
  final pageController = PageController();
  int currentIndex = 0;
  var appBarTitles = ['首页', '我的小组', '通知', '我的'];

  @override
  void initState() {
    super.initState();
    tabImages = [
      [
        getTabImage('lib/assets/images/tab/tab-home.png'),
        getTabImage('lib/assets/images/tab/tab-home-active.png')
      ],
      [
        getTabImage('lib/assets/images/tab/tab-alliance.png'),
        getTabImage('lib/assets/images/tab/tab-alliance-active.png')
      ],
      [
        getTabImage('lib/assets/images/tab/tab-notice.png'),
        getTabImage('lib/assets/images/tab/tab-notice-active.png')
      ],
      [
        getTabImage('lib/assets/images/tab/tab-me.png'),
        getTabImage('lib/assets/images/tab/tab-me-active.png')
      ],
    ];

    // 默认选中 pageController
    Future.delayed(Duration.zero, () {
      AppProvider appProvider = Provider.of<AppProvider>(context);
      currentIndex = appProvider.currentTabIndex;
      this.onTap(currentIndex);
      setState(() {});
    });
  }

  Text getTabTitle(int curIndex) {
    if (curIndex == currentIndex) {
      return new Text(appBarTitles[curIndex],
          style: new TextStyle(fontSize: 14.0, color: rgba(51, 51, 51, 1)));
    } else {
      return new Text(appBarTitles[curIndex],
          style: new TextStyle(fontSize: 14.0, color: rgba(136, 135, 135, 1)));
    }
  }

  Image getTabImage(path) {
    return new Image.asset(path, width: 24.0, height: 24.0);
  }

  Image getTabIcon(int curIndex) {
    if (curIndex == currentIndex) {
      return tabImages[curIndex][1];
    }
    return tabImages[curIndex][0];
  }

  void onTap(int index) {
    pageController.jumpToPage(index);
  }

  void onPageChanged(int index) {
    setState(() {
      currentIndex = index;
    });
    //  监听 tab 变化
    print('$index');
    if (index == 1) {
      // 刷新 我的小组信息
      _refreshGroup();
    }
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: HomeModel()),
      ],
      child: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              selectedFontSize: 14,
              unselectedFontSize: 14,
              selectedItemColor: rgba(51, 51, 51, 1),
              items: [
                BottomNavigationBarItem(icon: getTabIcon(0), label: '首页'),
                BottomNavigationBarItem(icon: getTabIcon(1), label: '我的小组'),
                BottomNavigationBarItem(icon: getTabIcon(2), label: '通知'),
                BottomNavigationBarItem(icon: getTabIcon(3), label: '我的'),
              ],
              currentIndex: currentIndex,
              onTap: onTap),
          body: PageView(
            controller: pageController,
            onPageChanged: onPageChanged,
            children: bodyList,
          )),
    );
  }

  void _refreshGroup() {
    Future.delayed(Duration.zero, () {
      _loadGroupInfo();
    });
  }

  void _loadGroupInfo() {
    G.showLoading();
    GroupFormDataModel formData = Provider.of<GroupFormDataModel>(context);
    formData.getMyGroup();
    G.dismissLoading();
  }
}
